package com.youdelu.basicframe.base.basedialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.gree.yipai.R;

/**
 * Create by 游德禄 on 2020-5-13
 */
public class PopDialog extends Dialog {
    public boolean marchParent;
    public View view ;
    private LayoutInflater inflater;
    public Context context;
    public PopDialog(@NonNull Context context) {
        this(context,-1,false);
    }
    public PopDialog(@NonNull Context context, int r) {
        this(context,r,false);
    }
    public PopDialog(@NonNull Context context, int r, boolean marchParent) {
        super(context);
        this.context = context;
        this.marchParent = marchParent;
        this.inflater = LayoutInflater.from(context);
        this.getContext().setTheme(android.R.style.Theme_InputMethod);
        if(r!=-1) {
            this.view = inflater.inflate(r, null);
        }
        if(view!=null) {
            setContentView(view);
        }else{
            setContentView(R.layout.dialog_base);
        }

        Window window = getWindow();
        WindowManager.LayoutParams attributesParams = window.getAttributes();
        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.5f;
        window.setAttributes(attributesParams);
        window.setLayout(marchParent?LinearLayout.LayoutParams.MATCH_PARENT:LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if(onDismiss!=null){
                    onDismiss.dismiss();
                }
            }
        });
    }

    public void setOutsideTouchable(boolean touchable){
      setCanceledOnTouchOutside(touchable);
    }
    public void setTouchable(boolean touchable){
        setCancelable(touchable);
    }
    public void setFocusable(boolean focusable){

    }
    public View getView(){
        return view;
    }
    public com.gree.yipai.widget.PopDialog setOnDissmissListener(OnDismiss onDismiss){
        this.onDismiss = onDismiss;
        return this;
    }
    /**
     * 关闭
     */
    private OnDismiss onDismiss;
    public interface OnDismiss{
        void dismiss();
    }

    private int position;
    /**
     * 获得打开时的位置
     * @return
     */
    public int getPosition(){
        return position;
    }
    /**
     * 显示
     */
    public void  show(){
        getWindow().setGravity(Gravity.BOTTOM);
        super.show();
    }

    /**
     * 显示
     */
    public void show(int position){
        show(position, Gravity.BOTTOM);
    }
    public void show(int position,int gravity){
        this.position = position;
        getWindow().setGravity(gravity);
        super.show();
    }
}
